package com.fiume.travel.web;

import com.fiume.travel.enums.StatusEnum;
import com.fiume.travel.exception.ProjectException;
import com.fiume.travel.res.ResponseWrap;
import com.fiume.travel.service.SellerService;
import com.fiume.travel.utils.ExceptionsUtil;
import com.fiume.travel.vo.SellerVo;
import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * Description:
 * </p>
 *
 * @author fiume
 * @version v1.0.0
 * @create 2021-02-23 16:00
 * @see com.fiume.travel.web
 */
@RestController
@RequestMapping("/seller")
@Log4j2
@CrossOrigin
@Api(tags = "商家模块")
public class SellerController {

    @Autowired
    SellerService sellerService;

    /***
     * 注册商家用户
     * @param sellerVo 注册信息
     * @return java.lang.Boolean
     */
    @PostMapping("/register")
    public ResponseWrap<Boolean> registerSeller(@RequestBody SellerVo sellerVo) throws ProjectException {
        try {
            Boolean flag = sellerService.registerSeller(sellerVo);
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("商家用户注册：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.REGISTER_USER_FAIL.getCode(),
                    StatusEnum.REGISTER_USER_FAIL.getMsg());
        }

    }

    /***
     * 商家用户登录
     * @param sellerVo 登录信息：账号和密码不能为空
     * @return com.itheima.travel.req.SellerVo
     */
    @PostMapping("/login")
    public ResponseWrap<SellerVo> loginSeller(@RequestBody SellerVo sellerVo) throws ProjectException {
        try {
            SellerVo sellerVoResult = sellerService.loginSeller(sellerVo);
            return ResponseWrap.<SellerVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(sellerVoResult)
                    .build();
        }catch (Exception e){
            log.error("商家用户登录：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.LOGIN_USER_FAIL.getCode(),
                    StatusEnum.LOGIN_USER_FAIL.getMsg());
        }

    }

    /***
     * 商家用户退出
     * @return com.itheima.travel.req.SellerVo
     */
    @GetMapping("/logout")
    public ResponseWrap<Boolean> loginOutUser() throws ProjectException {
        Boolean flag = true;
        try {
            sellerService.loginOutSeller();
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("是否登录：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FAIL.getCode(),
                    StatusEnum.FAIL.getMsg());
        }
    }

    /***
     * 商家用户是否登录
     * @return java.lang.Boolean
     */
    @GetMapping("/islogin")
    public ResponseWrap<Boolean> isLogin() throws ProjectException {
        try {
            Boolean flag = sellerService.isLogin();
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("是否登录：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.ISLOGIN_FAIL.getCode(),
                    StatusEnum.ISLOGIN_FAIL.getMsg());
        }

    }

    /***
     * @description 按id查询商家信息
     * @return: java.lang.Boolean
     */
    @GetMapping("/{sellerId}")
    public ResponseWrap<SellerVo> findSellerById(@PathVariable("sellerId") Long sellerId) throws ProjectException {
        try {
            SellerVo sellerVo = SellerVo.builder().id(sellerId).build();
            SellerVo sellerVoById = sellerService.findSellerVoById(sellerVo);
            return ResponseWrap.<SellerVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(sellerVoById)
                    .build();
        }catch (Exception e){
            log.error("商家信息：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FAIL.getCode(),
                    StatusEnum.FAIL.getMsg());
        }
    }
}
